﻿#include<stdio.h>
void jishu(int a[][20], int, int, int);
void chusi(int a[][20], int, int);

int num[20][20];
int ber[4][20][20];
int main(void)
{
	int n, k = 1;

	printf("请输入幻方的阶数: ");
	scanf("%d", &n);
	if (n % 2 == 1)
	{
		jishu(num, 1, n * n, n);
	}
	else if (n % 4 == 0)
	{
		chusi(num, 1, n);
	}
	else
	{
		int a = 0, b = 0;

		int N = n / 2;
		int k, t;
		jishu(ber[0], 1, N * N, N);
		jishu(ber[1], N * N + 1, N * N * 2, N);
		jishu(ber[2], N * N * 2 + 1, N * N * 3, N);
		jishu(ber[3], N * N * 3 + 1, n * n, N);
		k = (n - 2) / 4;
		for (int i = 0; i < k; i++)
		{
			for (int j = 0; j < N; j++)
			{
				if (j == N / 2)
					continue;
				else
				{
					t = ber[0][j][i];
					ber[0][j][i] = ber[3][j][i];
					ber[3][j][i] = t;
				}
			}
			t = ber[0][N / 2][i + N / 2];
			ber[0][N / 2][i + N / 2] = ber[3][N / 2][i + N / 2];
			ber[3][N / 2][i + N / 2] = t;
		}
		for (int i = 0, m = N / 2; i < k - 1; i++, m--)
		{
			for (int j = 0; j < N; j++)
			{
				t = ber[1][j][m];
				ber[1][j][m] = ber[2][j][m];
				ber[2][j][m] = t;
			}
		}
		for (int i = 0; i < 4; i++)
		{
			a = 0;
			b = 0;
			if (i == 1)
			{
				a = N;
				b = N;
			}
			if (i == 2)
			{
				b = N;
			}
			if (i == 3)
			{
				a = N;
			}

			for (int j = 0; j < N; j++)
			{
				for (int m = 0; m < N; m++)
				{
					num[j + a][m + b] = ber[i][j][m];
				}
			}

		}
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			printf("%2d ", num[i][j]);
		putchar('\n');
	}

	return 0;
}

void jishu(int a[][20], int min, int max, int n)
{
	int i, j;

	i = 0; j = n / 2;
	a[0][n / 2] = min;
	while (min <= max)
	{
		i--; j++;
		if (i < 0 && j == n)
		{
			i = i + 2;
			j = j - 1;
		}
		else if (j == n)
			j = 0;
		else if (a[i][j] != 0)
		{
			i = i + 2;
			j = j - 1;
		}
		else if (i < 0)
			i = n - 1;
		a[i][j] = ++min;
	}
}

void chusi(int a[][20], int min, int n)
{
	int i, j;
	int t, sijie, hang = 1, lie = 1, x, y;
	int b[50][4][4];

	sijie = n / 4;
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
			a[i][j] = min++;
	for (int o = 0; o < sijie * sijie; o++)
	{
		x = 0;
		for (i = hang - 1; i < 3 + hang; i++)
		{
			y = 0;
			for (j = lie - 1; j < lie + 3; j++)
			{
				b[o][x][y] = a[i][j];
				y++;
			}
			x++;
		}
		lie = lie + 4;
		if (j == n)
		{
			hang = hang + 4;
			lie = 1;
		}
	}
	for (int o = 0; o < sijie * sijie / 2; o++)
	{
		for (i = 0; i < 4; i++)
		{
			t = b[o][i][i];
			b[o][i][i] = b[sijie * sijie - 1 - o][4 - i - 1][4 - i - 1];
			b[sijie * sijie - 1 - o][4 - i - 1][4 - i - 1] = t;
			t = b[o][i][4 - i - 1];
			b[o][i][4 - i - 1] = b[sijie * sijie - 1 - o][4 - i - 1][i];
			b[sijie * sijie - 1 - o][4 - i - 1][i] = t;
		}
	}
	if (sijie % 2 == 1)
	{
		for (i = 0; i < 2; i++)
		{
			t = b[sijie * sijie / 2][i][i];
			b[sijie * sijie / 2][i][i] = b[sijie * sijie / 2][4 - i - 1][4 - i - 1];
			b[sijie * sijie / 2][4 - i - 1][4 - i - 1] = t;
			t = b[sijie * sijie / 2][i][4 - i - 1];
			b[sijie * sijie / 2][i][4 - i - 1] = b[sijie * sijie / 2][4 - i - 1][i];
			b[sijie * sijie / 2][4 - i - 1][i] = t;
		}
	}
	hang = 1;
	lie = 1;
	for (int o = 0; o < sijie * sijie; o++)
	{
		x = 0;
		for (i = hang - 1; i < 3 + hang; i++)
		{
			y = 0;
			for (j = lie - 1; j < lie + 3; j++)
			{
				a[i][j] = b[o][x][y];
				y++;
			}
			x++;
		}
		lie = lie + 4;
		if (j == n)
		{
			lie = 1;
			hang = hang + 4;
		}
	}
}